package com.cos.controller.system.apk;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cos.controller.base.BaseController;
import com.cos.entity.Page;
import com.cos.service.system.apk.ApkService;
import com.cos.util.Const;
import com.cos.util.DateUtil;
import com.cos.util.DelAllFile;
import com.cos.util.FileUpload;
import com.cos.util.PageData;
import com.cos.util.PathUtil;
import com.cos.util.Tools;

@Controller
@RequestMapping("/uploadFile")
public class UploadFileController extends BaseController{
	@Resource(name="apkService")
	private ApkService apkService ;
	
	/**
	 * 跳转上传页面
	 */
	@RequestMapping(value="/goToFile")
	public ModelAndView goToFile() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		mv.setViewName("system/apk/uploadAPK");
		mv.addObject("pd", pd);
		return mv;
	}

	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表Pictures");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	varList = apkService.listAll(pd);	//列出列表
			mv.setViewName("system/apk/apkList");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}


	@RequestMapping(value="/uploadAPK")
	public ModelAndView uploadAPK(@RequestParam(required=false)MultipartFile[] file) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		
		if (null != file && file.length != 0) {
			for(MultipartFile f:file) {
				String  ffile = "apkUpload", fileName = "";
				if(f.getSize() != 0) {
					DelAllFile.delFolder("D:/"+ Const.FILEPATHIMG + "apkUpload/apk.exe"); 	//删除图片
					//上传路径
					String filePath = "D:/" + Const.FILEPATHFILE + ffile;		//文件上传路径
					//数据库中保存的文件名（即文件真是名）
					fileName = FileUpload.fileUp(f, filePath, "apk");
					pd.put("ID", this.get32UUID());			//主键
					pd.put("URL", fileName);	//路径
					pd.put("DATETIME",Tools.date2Str(new Date()) ) ;
					//保存图片信息
					apkService.save(pd);
				}
			}
		}else{
			System.out.println("上传失败");
		}
		mv.setViewName("save_result");
		return mv ;
	}
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
